﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GameCommon;

namespace Tester
{
    class Tester
    {
    	private static int gameCount;
		private static string myTeamPath;
		private static string[] enemyTeamPath;

    	private static TesterSettings config;

        private static int overallWins;
        private static int overallDraws;
        private static int overallLoses;
        private static int overallScore1;
        private static int overallScore2;

		private static string ConcatDelimiter(string[] values, string delimiter)
		{
			for (int i = 0; i < values.Length - 1; i++)
				values[i] += delimiter;
			return string.Concat(values);
		}

        static void PlayGameSeries(string myTeam, string enemyTeam, bool fullStat)
		{
			//int top = Console.CursorTop;
			string enemyTeamName = Path.GetFileNameWithoutExtension(enemyTeam);

            if (fullStat)
                Console.WriteLine(enemyTeamName);

			int wins = 0;
			int draws = 0;
			int loses = 0;
            int sumScore1 = 0;
            int sumScore2 = 0;
			int exceptions = 0;
			for (int i = 0; i < gameCount; i++)
			{
				b engine = new b(myTeam, enemyTeam);
				
			    int score1;
			    int score2;
			    try
				{
					Score results = engine.d();
					if (results == null)
					{
						exceptions++;
						Logger.Log("results is null");
						continue;
					}
					
                    score1 = results.Team1Score;
                    score2 = results.Team2Score;

					if (score1 > score2) wins++;
					else if (score1 == score2) draws++;
					else loses++;

				    sumScore1 += score1;
                    sumScore2 += score2;

					engine.e();// CleanUpTeams();
					//engine.Dispose();
				}
				catch (Exception e)
				{
					exceptions++;
					Logger.Log(e.Message);
					continue;
				}

                if (!fullStat)
                {
                    Console.SetCursorPosition(0, Console.CursorTop);
                    Console.Write("{0}: +{1}={2}-{3}\t{6}-{7}\t{4}/{5}\t{8}", enemyTeamName, wins, draws, loses, i + 1, gameCount,
                                  sumScore1, sumScore2, exceptions > 0 ? "errors: " + exceptions : "");
                }
                else
                {
                    Console.WriteLine("{0}. {1}-{2} {3}", i + 1, score1, score2, score1 > score2 ? "+win" : (score1 == score2 ? "=draw" : "-lose"));
                }
			}

            overallWins += wins;
            overallDraws += draws;
            overallLoses += loses;
            overallScore1 += sumScore1;
            overallScore2 += sumScore2;

            if (!fullStat)
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.WriteLine("{0}: +{1}={2}-{3}\t{4}-{5}\t{6}\t", enemyTeamName, wins, draws, loses,
                                  sumScore1, sumScore2, exceptions > 0 ? "errors: " + exceptions : "");
            }
            else
            {
                Console.WriteLine("--------summary--------");
                Console.WriteLine("+{0}={1}-{2}\t{3}-{4}\t{5}\t", wins, draws, loses,
                                  sumScore1, sumScore2, exceptions > 0 ? "errors: " + exceptions : "");
                Console.WriteLine("");
            }

		}

        static void Main(string[] args)
        {
			config = new TesterSettings();

			if (args.Length == 0)
			{
				Console.WriteLine("Loading last used parameters: {0}", config.Args);
                args = "10\tTeamIce03.dll\tTeamIce04.dll\tTeamIce03.dll\t-stat".Split('\t');//config.Args.Split('\t');
			}

        	if (args.Length < 3)
			{
				ShowArgsHelp();
				return;
			}

            config.Args = ConcatDelimiter(args, "\t");
            config.Save();

            List<string> argsList = args.ToList();

            bool fullStat = argsList.RemoveAll(s => s.ToUpper() == "-stat".ToUpper()) > 0;

            gameCount = Int32.Parse(argsList[0]);
            myTeamPath = argsList[1].Trim();
            enemyTeamPath = new string[argsList.Count - 2];
            for (int i = 2; i < argsList.Count; i++)
                enemyTeamPath[i - 2] = argsList[i].Trim();

        	foreach (string enemyTeam in enemyTeamPath)
                PlayGameSeries(myTeamPath, enemyTeam, fullStat);

            Console.WriteLine("--------overall summary--------");
            Console.WriteLine("+{0}={1}-{2}\t{3}-{4}\t", overallWins, overallDraws, overallLoses,
                              overallScore1, overallScore2);

            //Console.ReadLine();    
        }

    	private static void ShowArgsHelp()
    	{
			Console.WriteLine("[-stat] GameCount YourTeamPath EnemyTeamPath1 [EnemyTeamPath2] ...");
    	}
    }
}
